import java.util.LinkedList;


public class Equation {
    
    public class Clause {
        private int[] vars;
        private boolean[] varsNot;
        
        public Clause (int[] vars, boolean[] varsNot){
            this.vars=vars;
            this.varsNot=varsNot;
        }
        
        public boolean assignBitVector(BitVector vec) throws IllegalAccessException{
            int assign = 0;
            for (int i=0; i < 3; i++){
                int location =vars[i];
                int assign1 = vec.get(location);
                if (varsNot[i])
                    if (assign1 == 0)
                        assign1 = 1;
                    else
                        assign1 = 0;
                
                assign += assign1;
            }
            return (assign > 0);
        }
    }
    
    private LinkedList<Clause> clauses = new LinkedList<Clause>();
    
    public Equation (String eq){
        int[] varsArr = null;
        boolean[] notArr = null;
        eq = eq.substring(1, eq.length()-1);
        String[] eqClauses = eq.split("\\)\\(");
        for (int i = 0; i < eqClauses.length; i++) {
            varsArr = new int[3];
            notArr = new boolean[3];
            String[] vars = eqClauses[i].split("#");
            for (int j = 0; j < vars.length; j++) {
                if (vars[j].charAt(0) == '-'){
                    notArr[j] = true;
                    varsArr[j] = Integer.parseInt(vars[j].substring(1));
                }
                else{
                    notArr[j] = false;
                    varsArr[j] =Integer.parseInt(vars[j]);
                }
            }
            this.clauses.add(new Clause(varsArr, notArr));
        }
    }
    
    public int countSatisfiedClauses(BitVector vec) throws IllegalAccessException{
        int count=0;
        for (Clause clause : this.clauses) {
            if (clause.assignBitVector(vec))
                ++count;
        }
        return count;
    }
    
    public boolean isSatisfied(BitVector vec) throws IllegalAccessException{
        int count = countSatisfiedClauses(vec);
        if (count == this.clauses.size())
            return true;
        else
            return false;
    }
    
    public int getClauseSize()
    {
        return this.clauses.size();
    }
    
}
